﻿using System;
using System.Collections.Generic;
using System.ServiceModel.Syndication;
using System.Web.Mvc;
using System.Xml;
using MSAcademic.v2.Infrastructure.Article;
using MSAcademic.v2.Web.Models.Home;

using System.Configuration;

namespace MSAcademic.v2.Web.Controllers
{
    public class RssActionResult : ActionResult
    {
        public SyndicationFeed Feed { get; set; }

        public override void ExecuteResult(ControllerContext context)
        {
            context.HttpContext.Response.ContentType = "text/xml";

            var rssFormatter = new Rss20FeedFormatter(Feed);

            using (var writer = XmlWriter.Create(context.HttpContext.Response.Output))
            {
                rssFormatter.WriteTo(writer);
            }
        }
    }

    public partial class RssController : Controller
    {
        [HttpGet]
        public ActionResult Home()
        {
            var rssModel = _rssModelFactory();
            var feed = new SyndicationFeed {
                Title = new TextSyndicationContent(ConfigurationManager.AppSettings["SiteName"]),
                ImageUrl = new Uri(ConfigurationManager.AppSettings["SiteImage"]),
                Description = new TextSyndicationContent(ConfigurationManager.AppSettings["SiteDescription"]),
            };

            var newestArticles = _rssService.BuildLatestArticlesRss(rssModel.LatestArticles); //Najnoviji artikli
            //var hightlightedArticles = _rssService.BuildHightLightedArticlesRss(rssModel.ArticlesForSubSections);//Izdvojeni artikli iz svake content sekcije
            //var root = _rssService.HomeRssMerge(hightlightedArticles, newestArticles);

            feed.Links.Add(new SyndicationLink(new Uri("http://msacademic.hr/")));
            feed.Items = newestArticles;

            return new RssActionResult() { Feed = feed };
        }

        public ActionResult AuthorRss(int authorId)
        {
            var rssModel = _rssModelFactory();
            rssModel.UserId = authorId;

            var authorsArticles = _rssService.BuildAuthorRss(rssModel.ArticlesForAuthor);
            var feed = new SyndicationFeed
                           {
                               Items=authorsArticles,
                               Title = new TextSyndicationContent(ConfigurationManager.AppSettings["SiteName"] + rssModel.Author.FirstName+rssModel.Author.LastName),
                               ImageUrl = new Uri(ConfigurationManager.AppSettings["SiteImage"]),
                               Description = new TextSyndicationContent(ConfigurationManager.AppSettings["SiteDescription"]),
                
                           };
            return new RssActionResult {Feed = feed};
        }
        [HttpGet] //TODO tutoriali su ID=3
        public ActionResult TutorialsRss()
        {
            var rssModel = _rssModelFactory();
            rssModel.ContentSectionId = 1;

            var tutorialContentSectionRss = _rssService.BuildContentSectionRss(rssModel.ArticlesForContentSection, "Tutoriali");
            var feed = new SyndicationFeed {Items = tutorialContentSectionRss,Title = new TextSyndicationContent("Tutoriali")};

            return new RssActionResult() {Feed = feed};
        }

        [HttpGet]//TODO članci su ID=2
        public ActionResult ArticlesRss()
        {
            var rssModel = _rssModelFactory();
            rssModel.ContentSectionId = 1;

            var articleContentSectionRss = _rssService.BuildContentSectionRss(rssModel.ArticlesForContentSection, "Clanci");
            var feed = new SyndicationFeed { Items = articleContentSectionRss ,Title = new TextSyndicationContent("Clanci")};

            return new RssActionResult() { Feed = feed };
        }

        [HttpGet]//TODO novosti su ID=1
        public ActionResult NewsRss()
        {
            var rssModel = _rssModelFactory();
            rssModel.ContentSectionId = 1;

            var newsContentSectionRss = _rssService.BuildContentSectionRss(rssModel.ArticlesForContentSection,
                                                                              "Novosti");
            var feed = new SyndicationFeed { Items = newsContentSectionRss,Title = new TextSyndicationContent("Novosti")};

            return new RssActionResult() { Feed = feed };
        }

        [HttpGet]
        public ActionResult ArticleRss(int articleId)
        {
            var feed = new SyndicationFeed { Title = new TextSyndicationContent("Artikal") };
            var rssModel = _rssModelFactory();
            rssModel.ArticleId = articleId;
            if (rssModel.Article == null)
            {
                var dummy = new SyndicationItem("404", "Trazeni artikal ne postoji", new Uri("http://msacademic.hr"));
                feed.Items = new List<SyndicationItem> { dummy };
                return new RssActionResult() { Feed = feed };
            }

            var articleRss = _rssService.BuildArticleRss(rssModel.Article);
            var items = new List<SyndicationItem> {articleRss};

            feed.Items = items;

            return new RssActionResult() { Feed = feed };
        }

    }
     public partial  class RssController
     {
         private readonly Func<RssModel> _rssModelFactory;
         private readonly IArticleRssService _rssService;
         public RssController(Func<RssModel> rssModelFactory,IArticleRssService rssService)
         {
             _rssModelFactory = rssModelFactory;
             _rssService = rssService;
         }
         public RssController()
         {
             
         }
     }
}
